package com.retail.bbq.cola.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.retail.bbq.cola.entity.WechatCustomer;
import com.retail.bbq.cola.enums.DeleteStatusEnum;
import com.retail.bbq.cola.enums.ErrorCodeEnum;
import com.retail.bbq.cola.exception.BusinessException;
import com.retail.bbq.cola.mapper.CustomerAgreementInfoMapper;
import com.retail.bbq.cola.mapper.WechatCustomerMapper;
import com.retail.bbq.cola.service.LawsonService;
import com.retail.bbq.cola.service.UserBindService;
import com.retail.bbq.cola.vo.request.userbind.UserBindRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * @author : jie.zhou
 * @date : 2025/7/23
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class UserBindServiceImpl implements UserBindService {

    private final WechatCustomerMapper wechatCustomerMapper;
    private final LawsonService lawsonService;
    private final CustomerAgreementInfoMapper customerAgreementInfoMapper;

    @Override
    @Transactional
    public boolean bind(UserBindRequest request) {
        WechatCustomer cola = wechatCustomerMapper.selectOne(
                new LambdaQueryWrapper<WechatCustomer>().eq(WechatCustomer::getOneid, request.getOneid())
                        .eq(WechatCustomer::getIsDel, DeleteStatusEnum.no_del.getCode())
                        .last("limit 1")
        );
        if (Objects.isNull(cola)) {
            throw new BusinessException(ErrorCodeEnum.COLA_USER_NOT_EXIST);
        }
        // 绑定关系已存在
        if (Objects.equals(cola.getLawsonId(), request.getLawsonId())) {
            log.info("用户已绑定");
            // 更新协议绑定
            updateAgreement(cola.getOpenId(), cola.getOneid(), request.getLawsonId());
            return true;
        }
        WechatCustomer lawson = wechatCustomerMapper.selectOne(
                new LambdaQueryWrapper<WechatCustomer>().eq(WechatCustomer::getLawsonId, request.getLawsonId())
                        .eq(WechatCustomer::getIsDel, DeleteStatusEnum.no_del.getCode())
                        .last("limit 1")
        );
        if (Objects.nonNull(lawson)) {
            // 该lawsonId存在且与该可乐没有绑定关系，删除旧绑定关系
            removeLawsonBind(request.getOneid(), request.getLawsonId());
        }
        wechatCustomerMapper.updateById(new WechatCustomer().setId(cola.getId()).setLawsonId(request.getLawsonId()));
        // 更新协议绑定
        updateAgreement(cola.getOpenId(), cola.getOneid(), request.getLawsonId());
        // 双通会员绑定成功通知
//        lawsonService.syncMember(new LawsonSyncMemberRequest().setOrgUserId(cola.getOneid()).setUserId(request.getLawsonId()));
        return true;
    }

    private void updateAgreement(String openId, String oneid, String lawsonId) {
        customerAgreementInfoMapper.updateBind(openId, oneid, lawsonId);
    }

    @Override
    public void removeLawsonBind(String oneid, String lawsonId) {
        if (StringUtils.isBlank(lawsonId)) {
            return;
        }
        wechatCustomerMapper.removeLawsonBind(oneid, lawsonId);
    }

}
